package io.github.mcsdk.ui.data;

import java.util.List;

/**
 * Data provider interface for UI components
 *
 * <p>Provides a contract for data access and manipulation in UI components.</p>
 *
 * @param <T> The type of data items
 * @author MC SDK Team
 * @version 1.0.0
 * @since 1.0.0
 */
public interface IDataProvider<T> {

    /**
     * Gets all data items
     *
     * @return List of all items
     */
    List<T> getData();

    /**
     * Sets the data items
     *
     * @param data New data list
     */
    void setData(List<T> data);

    /**
     * Gets a single item by index
     *
     * @param index Item index
     * @return The item at the specified index
     * @throws IndexOutOfBoundsException if index is out of range
     */
    T getItem(int index);

    /**
     * Gets the total number of items
     *
     * @return Item count
     */
    int getItemCount();

    /**
     * Checks if data is empty
     *
     * @return true if no items exist
     */
    default boolean isEmpty() {
        return getItemCount() == 0;
    }

    /**
     * Adds a new item to the data
     *
     * @param item Item to add
     */
    default void addItem(T item) {
        List<T> data = getData();
        data.add(item);
        setData(data);
    }

    /**
     * Removes an item from the data
     *
     * @param index Index of item to remove
     * @return The removed item
     */
    default T removeItem(int index) {
        List<T> data = getData();
        T removed = data.remove(index);
        setData(data);
        return removed;
    }

    /**
     * Clears all data
     */
    default void clear() {
        getData().clear();
        setData(getData());
    }
}